package com.func.domain.activity.service.quota;

import com.func.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import com.func.domain.activity.model.entity.*;
import com.func.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import com.func.domain.activity.repository.IActivityRepository;
import com.func.domain.activity.service.IRaffleActivitySkuStockService;
import com.func.domain.activity.service.quota.policy.ITradePolicy;
import com.func.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * RaffleActivityService
 * </p>
 *
 * @author Simon
 * @version 2025.03.12.1.0.0
 * @description RaffleActivityService 活动抽奖服务
 * @since 2025-03-12
*/

@Service
public class RaffleActivityServiceAccountQuota extends AbstractRaffleActivityAccountQuota implements IRaffleActivitySkuStockService {

    public RaffleActivityServiceAccountQuota(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory, Map<String, ITradePolicy> tradePolicyGroup) {
        super(activityRepository, defaultActivityChainFactory, tradePolicyGroup);
    }


    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity,ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        // 订单实体对象
        ActivityOrderEntity activityOrderEntity = new ActivityOrderEntity();
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setUserId(skuRechargeEntity.getUserId());

        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setPayAmount(activitySkuEntity.getProductAmount());

        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        // 订单编号一般来说公司还要做额外的处理(雪花UUID算法), 这里我们直接随意取12位
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setOrderTime(new Date());

        return CreateQuotaOrderAggregate.builder()
                .activityOrderEntity(activityOrderEntity)
                .activityId(activityOrderEntity.getActivityId())
                .dayCount(activityCountEntity.getDayCount())
                .totalCount(activityCountEntity.getTotalCount())
                .monthCount(activityCountEntity.getMonthCount())
                .userId(activityOrderEntity.getUserId())
                .build();
    }

    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
        return activityRepository.queryRaffleActivityAccountDayPartakeCount(activityId, userId);
    }

    @Override
    public ActivityAccountEntity queryActivityAccountEntity(String userId, Long activityId) {
        return activityRepository.queryActivityAccountEntity(userId, activityId);
    }

    @Override
    public void updateOrder(DeliveryOrderEntity deliveryOrderEntity) {
        activityRepository.updateCreditOrder(deliveryOrderEntity);
    }

    @Override
    public ActivitySkuStockKeyVO takeQueueValue() throws InterruptedException {
        return activityRepository.takeQueueValue();
    }

    @Override
    public void clearQueueValue() {
        activityRepository.clearQueueValue();
    }

    @Override
    public void updateActivitySkuStock(Long sku) {
        activityRepository.updateActivitySkuStock(sku);
    }

    @Override
    public void clearActivitySkuStock(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }
}
